package com.wondersgroup.fjzlpt.org.dao.impl.hzgl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.quick.framework.util.FormatDate;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.hzgl.YybDao;


/**
 * 预约表Dao实现类
 * @author gzb   
 * 2017年5月24日
 */
@Repository
public class YybDaoImpl extends BaseJTDAOImpl implements YybDao{

	@Override
	@Autowired
	@Qualifier("TB_HZGL_YYB")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	@Override
	public JSONObject queryPage(Map<String, String> map) {
		StringBuffer sqlBuf = new StringBuffer();
		
		sqlBuf.append("select a.YYBID,a.YYRQ,a.ZDBM,a.JZRQ,a.SFLY,");
		sqlBuf.append("b.XM,b.ZJHM,b.SJHM,b.CSRQ,b.XB,b.JTZZ,b.ZJLX,");
		sqlBuf.append("c.TDMC SSTDMC,");
		sqlBuf.append("(select YLJGMC from TB_ZD_YLJGXX where PTJGID=a.YYYYID) YYYYMC, \n"); //预约医院名称
		sqlBuf.append("(select KSMC from TB_ZD_KSXX where PTJGID=a.YYYYID and KSDM=a.YYKSID) YYKSMC \n");//预约科室名称
		sqlBuf.append("from TB_HZGL_YYB a left join TB_JTQY_JMXX b on a.JMXXID=b.JMXXID \n");
		sqlBuf.append("left join TB_TDGL_TDJBXX c on a.SSTDID=c.TDID where 1=1 \n");

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<Object> sqlParams = new ArrayList<Object>();
		try {
			//所属团队id
			String value = map.get("SSTDID");
			if(!StringUtils.isEmpty(value)){
				sqlBuf.append("and a.SSTDID = ? ");
				sqlParams.add(value);
			}
			//预约日期
			value = map.get("YYRQ_start");
			if(!StringUtils.isEmpty(value)){
				sqlBuf.append("and a.YYRQ >= ? ");
				sqlParams.add(sdf.parse(value));
			}
			value = map.get("YYRQ_end");
			if(!StringUtils.isEmpty(value)){
				sqlBuf.append("and a.YYRQ <= ? ");
				sqlParams.add(sdf.parse(value));
			}
			//预约医院id
			value = map.get("YYYYID");
			if(!StringUtils.isEmpty(value)){
				sqlBuf.append("and a.YYYYID = ? ");
				sqlParams.add(value);
			}
			//就诊日期
			value = map.get("JZRQ_start");
			if(!StringUtils.isEmpty(value)){
				sqlBuf.append("and a.JZRQ >= ? ");
				sqlParams.add(sdf.parse(value));
			}
			value = map.get("JZRQ_end");
			if(!StringUtils.isEmpty(value)){
				sqlBuf.append("and a.JZRQ <= ? ");
				sqlParams.add(sdf.parse(value));
			}
			//是否履约
			value = map.get("SFLY");
			if(!StringUtils.isEmpty(value)){
				sqlBuf.append("and a.SFLY = ? ");
				sqlParams.add(value);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int pageSize = Integer.parseInt(map.get("rows"));
		int pageNumber = Integer.parseInt(map.get("page"));
		PageQuerySQLBean pageBean = new PageQuerySQLBean(sqlBuf.toString(), pageSize, pageNumber, sqlParams.toArray());
		
		
		return super.queryPage(pageBean, ResultHandlerConstant.JSONARRAY);
	}

	@Override
	public JSONObject queryYyNumbers(Map<String, String> map) {
		StringBuffer sqlBuf = new StringBuffer("select count(YYBID) YYRC,sum(case when SFLY=1 then 1 else 0 end) LYRC from TB_HZGL_YYB where 1=1 ");
		
		List<Object> sqlParams = new ArrayList<Object>();
		
		//所属团队id
		String value = map.get("SSTDID");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append("and SSTDID = ? ");
			sqlParams.add(value);
		}
		//就诊日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		value = map.get("JZRQ_start");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append("and JZRQ >= ? ");
			sqlParams.add(FormatDate.getDate("yyyy-MM-dd",value));
		}
		value = map.get("JZRQ_end");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append("and JZRQ <= ? ");
			sqlParams.add(FormatDate.addDay(FormatDate.getDate("yyyy-MM-dd",value),1,"yyyy-MM-dd HH:mm:ss"));
		}
		
		SQLBean bean = new SQLBean(sqlBuf.toString(),sqlParams.toArray());
		
		return super.queryOne(bean);
	}
}
